library SelectDMCommand initializer init requires LegacySystem, PlayerSelectsHeroCommand, LegacyHumanAbilities

	private function Trig_Player_Selects_MM_Conditions takes nothing returns boolean
		if ( not ( GetUnitTypeId(GetTriggerUnit()) == 'H003' ) ) then
			return false
		endif
		return true
	endfunction


	private function Trig_Player_Selects_MM_Actions takes nothing returns nothing

		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer id = GetConvertedPlayerId(p)
		
		call putUnit(p,"hero", u)

		call setPlayerDataInteger(p,"pistol_ammo",8)
		call setPlayerDataInteger(p,"pistol_type",0)
		call setPlayerDataBoolean(p,"pistol_reload",false)

		call SetUnitUserData(u,20)
		  
		call putUnit(null,"MMUnit", u)
		call putBoolean(null,"MMPresent",true)

		set udg_KillFactor[id] = 0.8   
		set udg_PlayerIcon[id] = "ReplaceableTextures\\CommandButtons\\BTN_Unit_Marksman.blp"
		set udg_HeroAvailable[4] = 0
	  
		call AddPrimaryAmmo(u, 15, 20)
		call RegisterAmmoAutoReloadListener(u)
		call RegisterAmmoEmptyListener(u)
		
		call InitHero(u)
		
	  	if GetI("player mos", id) == 6 then
			call ModifyHeroSkillPoints( u, bj_MODIFYMETHOD_ADD, 1 )
		endif
		
		//call TriggerRegisterUnitEvent( gg_trg_Marksman_Scope, u, EVENT_UNIT_SPELL_EFFECT )
		
		
		set u = null
		set p = null
		
		if( udg_HeroAvailable[4] <= 0 ) then
			call DestroyTrigger(GetTriggeringTrigger())
		endif

	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterEnterRectSimple( t, GetPlayableMapRect() )
		call TriggerAddCondition( t, Condition( function Trig_Player_Selects_MM_Conditions ) )
		call TriggerAddAction( t, function Trig_Player_Selects_MM_Actions )
	endfunction

endlibrary